package com.peaker.advertisingpro.utils

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.GradientDrawable
import android.media.MediaPlayer
import android.net.Uri
import android.os.Handler
import android.view.Gravity
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.*
import android.widget.RelativeLayout.ALIGN_BOTTOM
import android.widget.RelativeLayout.ALIGN_TOP
import com.peaker.advertisingpro.bean.InterPlayBean
import com.peaker.advertisingpro.db.AppDatabase
import com.peaker.advertisingpro.entity.ViewSetObject
import com.peaker.advertisingpro.view.TextViewScroll
import java.io.File
import java.util.*


/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/3/21.
 *  代码人生
 */
object NewShowViewUtils {
    /**
     * 添加textview
     */
    fun newTextView(viewObject: ViewSetObject) {

        var textView = TextViewScroll(viewObject.context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = viewObject.width
        layoutParams.height = viewObject.height
        layoutParams.leftMargin = viewObject.leftMargin
        layoutParams.topMargin = viewObject.topMargin
        textView.layoutParams = layoutParams
        //字体
        viewObject.textFont?.let {
//            textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "${it}.ttf")
            when (it) {
                "SimSun" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}FangSong.ttf")
                }
                "FangSong" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}FangSong.ttf")
                }
                "SimHei" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}SimHei.ttf")
                }
                "Microsoft YaHei" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}MicrosoftYaHei.ttf")
                }
                "YouYuan" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}YouYuan.ttf")
                }
                "LiSu" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}LiSu.ttf")
                }
                "KaiTi" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}KaiTi.ttf")
                }
                "STCaiyun" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}STCaiyun.ttf")
                }
                "Arial" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}Arial.ttf")
                }
                "Tohama" -> {
                    textView.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}Tohama.ttf")
                }
            }


        }
//        textView.id = viewObject.viewId

        viewObject.fontStyle?.let {
            if(it=="italic"){
                textView.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC)
            }
        }
        viewObject.fontWeight?.let {
            if(it=="bold"){
                textView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD)
            }

            if( viewObject.fontStyle=="italic"){
                textView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC)
            }
        }
        viewObject.textDecoration?.let {
            if(it=="underline"){
                textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG)
            }
            if(it=="line-through"){
                textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG)
            }
        }
        textView.text = viewObject.path
        textView.setTextColor(viewObject.textColor)
        textView.setBackgroundColor(viewObject.backGroudColor)
        textView.textSize = viewObject.textSize
        viewObject.textAlign.let {
            when (it) {
                "center" -> textView.gravity = Gravity.CENTER
                "left" -> textView.gravity = Gravity.LEFT
                "right" -> textView.gravity = Gravity.RIGHT
                "bottom" -> textView.gravity = Gravity.BOTTOM
                "top" -> textView.gravity = Gravity.TOP
            }
        }
        viewObject.rootView.addView(textView)
        textView.setOnClickListener {
            viewObject.funtion?.let {
                it()
            }
        }
//        0123  //静态文本 向上滚动 向下滚动 向左
        textView.isScrollStatus = true
        when(viewObject.direction){
            "0"->{
                textView.isScrollStatus = false
            }
            "1"->{
                textView.scrollType = TextViewScroll.FROM_BOTTOM
            }
            "2"->{
                textView.scrollType = TextViewScroll.FROM_TOP
            }
            "3"->{
                textView.scrollType = TextViewScroll.FROM_RIGHT
            }

        }
        //12345 从快到慢
        when (viewObject.speed) {
            "1" -> textView.speed = 5
            "2" -> textView.speed = 4
            "3" -> textView.speed = 3
            "4" -> textView.speed = 2
            "5" -> textView.speed = 1
        }

    }

    /**
     * 插播文本
     */
    fun newTextView2(viewObject: InterPlayBean, context: Context, rootView: ViewGroup) {

        var textView = TextViewScroll(context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )

        if (viewObject.position == "2") {
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
        } else {
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        }
        textView.layoutParams = layoutParams

        textView.text = viewObject.content
        viewObject.fontColor?.let {
        textView.setTextColor(Color.parseColor(Utils.fixColor(it)))
        }
        var toDouble = viewObject.bgOpacity?.toDouble()
        var trans = Integer.toHexString((toDouble!! * 255).toInt())
        trans = if (trans.length == 1) "0$trans" else trans
        viewObject.bgColor?.let {
            var replace = it.replace("#", trans)
            textView.setBackgroundColor(Color.parseColor("#$replace"))
        }


        viewObject.fontSize?.toFloat()?.let {
            textView.textSize = it
        }
        if (viewObject.direction == "1") {
            textView.scrollType = TextViewScroll.FROM_RIGHT
        } else {
            textView.scrollType = TextViewScroll.FROM_LEFT
        }

        when (viewObject.speed) {
            "1" -> textView.speed = 1
            "2" -> textView.speed = 3
            "3" -> textView.speed = 5
        }
        textView.isScrollStatus = true


        var num: Int = viewObject.numbered

        if (viewObject.type == "1") {//安时间播放
            var isPlay = DateUtils.isEffectiveDate(
                Date(),
                DateUtils.getDateByStr2(viewObject.startTime!!),
                DateUtils.getDateByStr2(viewObject.endTime!!)
            )
            textView.isScrollStatus = isPlay
            if (isPlay) {
                rootView.addView(textView)
            }

        } else {
           if(viewObject.number>viewObject.numbered){
               rootView.addView(textView)
           }

        }

        textView.setOnScrollNumListener {
            if (viewObject.type == "1") {//安时间播放
                var isPlay = DateUtils.isEffectiveDate(
                    Date(),
                    DateUtils.getDateByStr2(viewObject.startTime!!),
                    DateUtils.getDateByStr2(viewObject.endTime!!)
                )
                textView.isScrollStatus = isPlay
                if (isPlay.not()) {
                    rootView.removeView(textView)
                }

            } else {
                num++
                //更新播放次数
             AppDatabase.instance.interPlayDao().updateNumber(viewObject.id,num)
                if (num > viewObject.number) {
                    textView.isScrollStatus = false
                    rootView.removeView(textView)
                }

            }
        }

    }

    /**
     * 添加videoView
     */
    fun newVideoView(viewObject: ViewSetObject) {

        val videoView = VideoView(viewObject.context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = viewObject.width
        layoutParams.height = viewObject.height
        layoutParams.leftMargin = viewObject.leftMargin
        layoutParams.topMargin = viewObject.topMargin
        videoView.layoutParams = layoutParams
        videoView.id = viewObject.viewId
        videoView.setVideoPath(viewObject.path)
        videoView.start()
        videoView.setOnPreparedListener {
            it.start()
            it.isLooping = true
        }
        viewObject.rootView.addView(videoView)
    }

    /**
     * 添加imageView
     */
    fun newImageView(viewObject: ViewSetObject) {
        val imageView = ImageView(viewObject.context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = viewObject.width
        layoutParams.height = viewObject.height
        layoutParams.leftMargin = viewObject.leftMargin
        layoutParams.topMargin = viewObject.topMargin
//        imageView.setImageResource(R.mipmap.ic_launcher)
        if (viewObject.type == "pdf") {
            imageView.setImageBitmap(viewObject.bitmap)
        } else {
            imageView.setImageURI(Uri.fromFile(File(viewObject.path)))
        }
        imageView.scaleType = ImageView.ScaleType.FIT_XY
        imageView.layoutParams = layoutParams
        imageView.id = viewObject.viewId
        viewObject.rootView.addView(imageView)

        if (viewObject.isClick) {
            imageView.setOnClickListener {

            }
        }

    }

    /**
     * 创建webview
     */
    fun newWebView(viewObject: ViewSetObject) {
        val webview = WebView(viewObject.context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = viewObject.width
        layoutParams.height = viewObject.height
        layoutParams.leftMargin = viewObject.leftMargin
        layoutParams.topMargin = viewObject.topMargin

        webview.elevation = viewObject.zIndex.toFloat()
        webview.layoutParams = layoutParams
        webview.id = viewObject.viewId
        viewObject.rootView.addView(webview)
        webview.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }
        })
        webview.loadUrl(viewObject.path)
        var handler = Handler()
        handler.postDelayed(Runnable {
            webview.reload()
        }, viewObject.time)

        viewObject.funtion?.let {
            webview.setOnClickListener {view->
                    it()
            }
        }


    }


    fun newBtnView(viewObject: ViewSetObject) {
        val btn = Button(viewObject.context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = viewObject.width
        layoutParams.height = viewObject.height
        layoutParams.leftMargin = viewObject.leftMargin
        layoutParams.topMargin = viewObject.topMargin
        btn.layoutParams = layoutParams
//        btn.id = viewObject.viewId
        btn.text = viewObject.path
        btn.setTextColor(viewObject.textColor)
        btn.textSize = viewObject.textSize

//        btn.setrad
        viewObject.textFont?.let {
            var replace = it.replace(" ", "")
            btn.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}${replace}.ttf")
        }
        viewObject.textAlign.let {
            when (it) {
                "center" -> btn.gravity = Gravity.CENTER
                "left" -> btn.gravity = Gravity.LEFT
                "right" -> btn.gravity = Gravity.RIGHT
                "bottom" -> btn.gravity = Gravity.BOTTOM
                "top" -> btn.gravity = Gravity.TOP
            }
        }

        btn.setOnClickListener {
            viewObject.funtion?.let {
                it()
            }
        }

        val gd = GradientDrawable()
        gd.cornerRadius = viewObject.borderRadius
        gd.setColor(viewObject.backGroudColor)
        btn.background = gd
        viewObject.rootView.addView(btn)
    }

    /**
     * 时间
     */
    fun newTimeView(viewObject: ViewSetObject) {

        val textClock = TextClock(viewObject.context)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = viewObject.width
        layoutParams.height = viewObject.height
        layoutParams.leftMargin = viewObject.leftMargin
        layoutParams.topMargin = viewObject.topMargin
        textClock.layoutParams = layoutParams

        if (textClock.is24HourModeEnabled){
            textClock.format24Hour = viewObject.path
        }else {
            textClock.format12Hour = viewObject.path
        }
        viewObject.textFont?.let {
            var f=it.replace(" ","")
            textClock.typeface = Typeface.createFromAsset(viewObject.context.assets, "font${File.separator}${f}.ttf")
        }

        textClock.setTextColor(viewObject.textColor)
        textClock.setBackgroundColor(viewObject.backGroudColor)
        textClock.textSize = viewObject.textSize
        textClock.elevation = viewObject.zIndex.toFloat()

        viewObject.textAlign.let {
            when (it) {
                "center" -> textClock.gravity = Gravity.CENTER
                "left" -> textClock.gravity = Gravity.LEFT
                "right" -> textClock.gravity = Gravity.RIGHT
                "bottom" -> textClock.gravity = Gravity.BOTTOM
                "top" -> textClock.gravity = Gravity.TOP
            }
        }
        textClock.setOnClickListener {
            viewObject.funtion?.let {
                it()
            }
        }
        viewObject.rootView.addView(textClock)
    }

    /**
     * 移除view
     */
    fun removeView(viewObject: ViewSetObject) {

        viewObject.rootView.removeView(viewObject.rootView.findViewById(viewObject.viewId))
    }

    /**
     * 关闭播放
     */
    fun closeMedia(mediaplayer: MediaPlayer) {
//        var mediaplayer = mediaplayer
        mediaplayer.stop()
        mediaplayer.reset()
        mediaplayer.release()
    }

}